package com.cloudm.technician.ui.modules.cloudm_box.upload_pic

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import com.cloudm.technician.R
import com.cloudm.technician.data.model.ImageInfo
import com.cloudm.technician.extensions.getScreenWidth
import com.cloudm.technician.extensions.inflate
import com.cloudm.technician.extensions.loadImage
import com.cloudm.technician.extensions.logger
import com.cloudm.technician.ui.widget.OnceClickListener
import kotlinx.android.synthetic.main.item_upload_pic.view.*

/**
 * 作者: wesley
 * 时间: 2017/12/22
 * 描述: 添加照片的适配器
 */
class UploadPicAdapter(context: Context, val flag: Int = 0) :
        RecyclerView.Adapter<UploadPicAdapter.ItemViewHolder>() {

    private val params: RecyclerView.LayoutParams

    var listener: OnItemClickListener? = null

    init {
        val space = context.resources.getDimensionPixelSize(R.dimen.common_margin_16dp) * 4
        val size = (context.getScreenWidth() - space) / 3
        params = RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, size)
    }

    companion object {
        private const val TYPE_0 = 0
        private const val TYPE_1 = 1
        private const val TYPE_2 = 2
    }

    val list: ArrayList<ImageInfo> = arrayListOf()

    fun addAllItems(list: ArrayList<ImageInfo>) {
        this.list.clear()
        this.list.addAll(list)
        notifyDataSetChanged()
    }

    fun updateUi(position: Int, flag: Int, url: String?) {
        if (position < 0 || list.size <= position || url == null) {
            logger(" position $position list.size = ${list.size} url = $url")
            return
        }
        list[position].flag = flag
        list[position].url = url
        notifyItemChanged(position)
    }


    fun updateUi(position: Int, flag: Int, percent: Double) {
        if (position < 0 || list.size <= position) {
            logger(" position $position list.size = ${list.size}")
            return
        }
        list[position].flag = flag
        list[position].percent = percent
        notifyItemChanged(position)
    }

    override fun getItemViewType(position: Int): Int {
        if (list.size <= position) {
            return TYPE_0
        }
        return list[position].flag
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ItemViewHolder {
        return ItemViewHolder(parent?.inflate(R.layout.item_upload_pic))
    }

    override fun getItemCount(): Int = list.size

    override fun onBindViewHolder(holder: ItemViewHolder?, position: Int) {
        if (holder == null || list.size <= position) {
            return
        }
        holder.itemView.layoutParams = params
        val viewType = getItemViewType(position)
        val info = list[position]
        when (viewType) {
            TYPE_0 -> {
                holder.itemView.rlLocalPic.visibility = View.VISIBLE
                holder.itemView.ivItemPic.visibility = View.GONE
                holder.itemView.tvItemValueShadow.visibility = View.GONE
                holder.itemView.tvName.text = info.value
                holder.itemView.tvItemPercent.visibility = View.GONE
            }

            TYPE_1, TYPE_2 -> {
                holder.itemView.rlLocalPic.visibility = View.GONE
                holder.itemView.ivItemPic.visibility = View.VISIBLE
                holder.itemView.ivItemPic.loadImage(info.url)
                when (flag) {
                    0 -> {
                        holder.itemView.tvItemValueShadow.visibility = View.VISIBLE
                        holder.itemView.tvItemValueShadow.text = info.value
                    }
                    1 -> holder.itemView.tvItemValueShadow.visibility = View.GONE
                }
                when (viewType) {
                    TYPE_1 -> {
                        holder.itemView.tvItemPercent.visibility = View.VISIBLE
                        holder.itemView.tvItemPercent.text = StringBuffer().append(
                                (info.percent * 100).toInt().toString()).append("%")
                    }
                    TYPE_2 -> holder.itemView.tvItemPercent.visibility = View.GONE
                }
            }
        }

        holder.itemView.setOnClickListener(object : OnceClickListener() {
            override fun onOnceClick(view: View) {
                listener?.onItemClick(info, holder.adapterPosition)
            }
        })
    }

    class ItemViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView)


    interface OnItemClickListener {

        fun onItemClick(info: ImageInfo, position: Int)
    }
}